package com.study.common.pagination;

import java.util.Collections;
import java.util.List;

/**
 * @Author 谭永超 [www.tanyongchao.tk:8090/SumDome/index]
 * @Date 2018/8/29 09:38
 * @Description 分页工具类
 */
public class Pagination {
    // 默认每页显示的记录条数
    private final Integer PAGE_DATA_limit = 10;
    // 记录总条数
    private Integer totalRows;
    // 记录总页数
    private Integer totalPage;
    // 当前的页数
    private Integer pageIndex;
    // 每一页显示的记录的条数
    private Integer limit;
    // 存放返回值的集合
    private List<?> list;
    //开始的行数
    private Integer startRow;

    public Integer getTotalRows() {
        return totalRows;
    }

    public void setTotalRows(Integer totalRows) {
        this.totalRows = totalRows;
        int totalPage = totalRows % getLimit() == 0 ? totalRows / getLimit() : totalRows / getLimit() + 1;
        setTotalPage(totalPage);
    }

    public Integer getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(Integer totalPage) {
        this.totalPage = totalPage;
    }

    public Integer getPageIndex() {
        pageIndex = pageIndex == null ? 0 : pageIndex;
        return pageIndex;
    }

    public void setPageIndex(Integer pageIndex) {
        this.pageIndex = pageIndex;
    }

    public final Integer getLimit() {
        return limit == null ? PAGE_DATA_limit : limit;
    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    public List<?> getList() {
        return list == null ? Collections.EMPTY_LIST : list;
    }

    public void setList(List<?> list) {
        this.list = list;
    }


    public Integer getStartRow() {
        if (startRow == null) {
            startRow = getPageIndex() == 0 ? 0 : (pageIndex - 1) * limit;
        }
        return startRow;
    }

    public void setStartRow(Integer startRow) {
        this.startRow = startRow;
    }
}
